﻿/*************************************************************************************
 * 工    具：  HK_Curve2DCtrl
 * 
 * 描    述：  自定义的UI组件，用于在UGUI中绘制2D曲线，并可控制曲线默认显示状态，以及沿不同方向生长出现
 * 
 * 版    本：  V1.0
 * 作    者：  京产肠饭
 * 
 * 创    建：  2024/01/25  V1.0
 * 
 * 链    接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public enum CurveGrowAnimType
{
    FromLeft,
    FromRight,
    FromTop,
    FromBottom,
}

public class HK_Curve2DCtrl : MonoBehaviour
{
    [Header("动画速度："), SerializeField, Range(0.2f, 2)] float animSpeed = 1;

    RectMask2D mask;
    RectTransform rtf;

    float startValue;

    void Awake()
    {
        mask = GetComponent<RectMask2D>();
        rtf = GetComponent<RectTransform>();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            PlayGrowAnim(CurveGrowAnimType.FromLeft);
        }

        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            PlayGrowAnim(CurveGrowAnimType.FromRight);
        }

        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            PlayGrowAnim(CurveGrowAnimType.FromTop);
        }

        if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            PlayGrowAnim(CurveGrowAnimType.FromBottom);
        }

        if (Input.GetKeyDown(KeyCode.F1))
        {
            SetCurveInitState(true);
        }

        if (Input.GetKeyDown(KeyCode.F2))
        {
            SetCurveInitState(false);
        }
    }

    /// <summary>
    /// 设置 曲线的默认显示状态
    /// </summary>
    /// <param name="isShow"></param>
    public void SetCurveInitState(bool isShow)
    {
        if (isShow)
        {
            mask.padding = new Vector4(0, 0, 0, 0);
        }
        else
        {
            startValue = rtf.rect.width;
            mask.padding = new Vector4(0, 0, startValue, 0);
        }
    }

    /// <summary>
    /// 调用曲线的生成动画
    /// </summary>
    /// <param name="animType"></param>
    public void PlayGrowAnim(CurveGrowAnimType animType)
    {
        switch (animType)
        {
            case CurveGrowAnimType.FromLeft:
                startValue = rtf.rect.width;
                mask.padding = new Vector4(0, 0, startValue, 0);
                StartCoroutine(PlayGrowAnim_IE(CurveGrowAnimType.FromLeft));
                break;
            case CurveGrowAnimType.FromRight:
                startValue = rtf.rect.width;
                mask.padding = new Vector4(startValue, 0, 0, 0);
                StartCoroutine(PlayGrowAnim_IE(CurveGrowAnimType.FromRight));
                break;
            case CurveGrowAnimType.FromTop:
                startValue = rtf.rect.height;
                mask.padding = new Vector4(0, startValue, 0, 0);
                StartCoroutine(PlayGrowAnim_IE(CurveGrowAnimType.FromTop));
                break;
            case CurveGrowAnimType.FromBottom:
                startValue = rtf.rect.height;
                mask.padding = new Vector4(0, 0, 0, startValue);
                StartCoroutine(PlayGrowAnim_IE(CurveGrowAnimType.FromBottom));
                break;
        }
    }

    IEnumerator PlayGrowAnim_IE(CurveGrowAnimType animType)
    {
        float time = 0;

        while (true)
        {
            time += Time.deltaTime * animSpeed;

            var tmp = Mathf.Lerp(startValue, 0, time);

            mask.padding = new Vector4(0, 0, tmp, 0);

            switch (animType)
            {
                case CurveGrowAnimType.FromLeft:
                    mask.padding = new Vector4(0, 0, tmp, 0);
                    break;
                case CurveGrowAnimType.FromRight:
                    mask.padding = new Vector4(tmp, 0, 0, 0);
                    break;
                case CurveGrowAnimType.FromTop:
                    mask.padding = new Vector4(0, tmp, 0, 0);
                    break;
                case CurveGrowAnimType.FromBottom:
                    mask.padding = new Vector4(0, 0, 0, tmp);
                    break;
            }

            if (time >= 1)
            {
                startValue = 0;
                mask.padding = new Vector4(0, 0, 0, 0);
                break;
            }

            yield return null;
        }
    }
}
